﻿//系统管理-->系统通知
//Create By:li 2014年9月29日17:24:40

//1.0 加载JqueryDatabase
jQuery(function ($) {
    var oTable1 = $('#sample-table-2').dataTable({
        "aoColumns": [
          { "bSortable": false },
          { "bSortable": true },
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": true },
          { "bSortable": true },
          //{ "bSortable": true },
          { "bSortable": false }
        ],
        oLanguage: {
            "sProcessing": "正在加载中......",
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "对不起，查询不到相关数据！",
            "sEmptyTable": "表中无数据存在！",
            "sInfo": "当前显示： _START_ 到 _END_ 条，共 _TOTAL_ 条",
            "sInfoFiltered": "数据表中共为 _MAX_ 条记录",
            "sSearch": "",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "末页"
            }
        },
        iDisplayLength: 30,
        bLengthChange: false
    });


    $('table th input:checkbox').on('click', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
        .each(function () {
            this.checked = that.checked;
            $(this).closest('tr').toggleClass('selected');
        });

    });


    $('[data-rel="tooltip"]').tooltip({ placement: tooltip_placement });
    function tooltip_placement(context, source) {
        var $source = $(source);
        var $parent = $source.closest('table')
        var off1 = $parent.offset();
        var w1 = $parent.width();

        var off2 = $source.offset();
        var w2 = $source.width();

        if (parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
        return 'left';
    }
})



window.onload = function () {


    //2.0 页面初始化
    //2.1 给table加按钮（增加，删除，发送，撤回，刷新）
//    var $aa = $("#sample-table-2_wrapper .col-sm-6").eq(0);
//    $aa.html("<button class='btn btn-primary btn-sm' id='butadd'   ><i class='icon-plus-sign bigger-110'></i><span class='bigger-110 no-text-shadow'>新增</span></button>");
//    $aa.append("<button class='btn btn-danger btn-sm' style='margin-left: 4px' id='butdel'><i class='icon-trash bigger-110'></i><span class='bigger-110 no-text-shadow'>删除</span></button>");
   // $aa.append("<button class='btn btn-sm btn-info no-radius' style='margin-left: 4px' id='butsend'><i class='icon-share-alt bigger-110'></i><span class='bigger-110 no-text-shadow'>发送</span></button>");
   // $aa.append("<button class='btn btn-sm' style='margin-left: 4px' id='butgetback'><i class='icon-undo bigger-110'></i><span class='bigger-110 no-text-shadow'>撤回</span></button>");
//    $aa.append("<button class='btn btn-success btn-sm' style='margin-left: 4px' id='buflush'><i class='icon-refresh bigger-110'></i><span class='bigger-110 no-text-shadow'>刷新</span></button>");
//    $("td").addClass("tdBorder");


    //2.2 table表头上的样式
    $("#sample-table-2>thead tr>th").attr("style", "font-weight:bold;color:black;text-align:center");


    //2.3 table搜索样式
    $("#sample-table-2_filter label input").attr("placeholder", "搜索(多条件搜索中间用空格隔开)").attr("style", "width:50%");
    $("#sample-table-2_filter label").attr("style", "width:100%");
  
    $("#sample-table-2_filter").attr("style", "margin-right: -16px;");
    $("#sample-table-2_wrapper .row").first().children().eq(1).attr("style", "margin-top:-12px");
    $("#sample-table-2_wrapper").attr("style", "margin-top:-32px");
    $("#butadd").attr("style", "margin-top:-4px");

    //2.4 增加表单行间距样式
    $("div .modal-body .row:not(#iconr)").attr("style", "margin-top:7px;");
    $("div .modal-body .row .col-sm-1").attr("style", "margin-top:6px;text-align:right");
    $("div#myModal .modal-body .row i:lt(1)").attr("style", "margin-top:9px;text-align:right;");

    $("#form_field_select_4_chosen").attr("style", "width:100%");
    //2.5 显示页数一行的样式
    $("#sample-table-2").next().find(".col-sm-6").eq(0).attr("style", "margin-left: -12px;margin-top: 5px;");
    $("#sample-table-2").next().find(".col-sm-6").eq(1).attr("style", "margin-left: 12px;margin-top: 0px;");


    //2.6 按钮样式
    $("button.btn.btn-sm:gt(0)").attr("style", "margin:-5px 0px 0px 4px");

    //3.2   刷新
    $("#buflush").click(function () {
        history.go(0);
    });
    $("#bgdiv").attr("style", "display:none");
    //3.3   点击新增按钮
    //3.3.1 点击新增按钮，初始化模态窗口
    function initForm() {
        $("div#myModal .modal-body .row i:lt(1)").attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
        $("div#myModal .modal-body .row input:not([autocomplete])").val("").removeAttr("style");
        $("div#myModal .modal-body .row .col-sm-10 .wysiwyg-editor:not([autocomplete])").val("").removeAttr("style");
        //$("div#myModal .modal-body .row select:lt(1)").val("0").removeAttr("style");

        $("#but_saveadd").removeAttr("style");
        $("#but_savemodify").removeAttr("style").attr("style", "display:none");


    } 
    //3.3.2 启动模态窗口
    $("#butadd").click(function () {
        initForm();
        $("#myModal").modal();
    });
    //3.3.3 验证表单
    $("#MSGTitle").focus(function () {
        $(this).removeAttr("style");
        $(this).parent().next().attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
    });
    $("#MSGTitle").blur(function () {
        var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{1,50}$/;  //长度为1-50的字母、数字、下划线、汉字
        var obj = $(this).val();
        if (!reg.test(obj)) {
            $(this).attr("style", " border-color: red;");
            $(this).parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
        }
    });

    $("#editor1").focus(function () {
        $(this).removeAttr("style");
        $(this).parent().next().attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
    });
    $("#editor1").blur(function () {
       
        if (document.getElementById("editor1").innerHTML.toString() == "") {
            $(this).attr("style", " border-color: red;");
            $(this).parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
        }
    });

    $("#s2id_autogen1").attr("style", "width: 302px; height: 31px; font-size: 15px; font-family: 微软雅黑;");



    //3.4  新增保存
    function save(sendflag) {


        /*
         *  下面两种获取节点内容的方式都可以。
         *  由于 IE6, IE7 不支持 contentDocument 属性，所以此处用了通用的
         *  window.frames["iframe Name"] or window.frames[index]
         */

        var sendflag = sendflag;
        var MSGTitle = $("#MSGTitle").val();
        var MSGCountent = document.getElementById("editor1").innerHTML.toString();
        var Status = $("#Status").val();

        var flag = true;

        if (1 == 1) {
            var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{1,50}$/;  //长度为1-50的字母、数字、下划线、汉字
            if (!reg.test(MSGTitle)) {
                flag = false;
                $("#MSGTitle").attr("style", " border-color: red;");
                $("#MSGTitle").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
                msg = "请重新填写通知标题之后再次提交！";
            }
        }
        if (1 == 1 && flag) {
            // var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{2,106}$/;  //长度为2-106的字母、数字、下划线、汉字
            //if (!reg.test(MSGCountent)) {
            if (MSGCountent == "") {
                flag = false;
                $("#editor1").attr("style", " border-color: red;");
                $("#editor1").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
                msg = "请重新填写通知内容之后再次提交！";
            }
        } 
        if (!flag) {
            $.gritter.add({
                title: '消息提醒',
                text: msg,
                class_name: 'gritter-info gritter-center'
            });
            return;
        }

        $.ajax({
            url: "AddNewMessage",
            dataType: "json",
            Type: "POST",
            data: {
                MSGID: sendflag,
                MSGTitle: MSGTitle,
                MSGCountent: MSGCountent,
                Status: Status

            },
            success: function (data) {
                var json = eval(data);
                if (json.result == "Success") {


                    $("#but_infoclose").click();
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });

                    history.go(0);

                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        });
    }
    $("#but_saveadd").click(function () {

        save(2);
    });

    
    //3.5.2 保存修改
    function savemodify(sendflag) {

        var MSGCountent = document.getElementById("editor1").innerHTML.toString();
        var MSGID = $("#idHidden").val();
        var MSGTitle = $("#MSGTitle").val();

        var Status = $("#Status").val();


        var flag = true;

        if (1 == 1) {
            var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{1,50}$/;  //长度为1-50的字母、数字、下划线、汉字
            if (!reg.test(MSGTitle)) {
                flag = false;
                $("#MSGTitle").attr("style", " border-color: red;");
                $("#MSGTitle").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
                msg = "请重新填写通知标题之后再次提交！";
            }
        }
        if (1 == 1 && flag) {
            // var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{2,106}$/;  //长度为2-106的字母、数字、下划线、汉字
            //if (!reg.test(MSGCountent)) {
            if (MSGCountent == "") {
                flag = false;
                $("#editor1").attr("style", " border-color: red;");
                $("#editor1").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
                msg = "请重新填写通知内容之后再次提交！";
            }
        }

        if (!flag) {
            $.gritter.add({
                title: '消息提醒',
                text: msg,
                class_name: 'gritter-info gritter-center'
            });
            return;
        }



        $.ajax({
            url: "SaveModifyMessage",
            dataType: "json",
            Type: "POST",
            data: {
                sendflag: sendflag,
                MSGID: MSGID,
                MSGTitle: MSGTitle,
                MSGCountent: MSGCountent,
                Status: Status
            },
            success: function (data) {

                var json = eval(data);
                if (json.result == "Success") {

                    //alert(json.obj);

                    $("#but_infoclose").click();
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    setTimeout(history.go(0), 1000);

                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        }
        );

    }
    $("#but_savemodify").click(function () {
        savemodify(2);
    }
    );



    //3.8 新建页面发送按钮
    $("#but_send").click(function () {
        //save参数为1就调用发送方法为2仅保存
        save(1);

    });
    //3.9 修改页面发送按钮
    $("#but_modifysend").click(function () {
        //savemodify参数为1就调用发送方法为2仅保存  
        savemodify(1);

    });

}

//3.3.1 点击新增按钮，初始化模态窗口
function initForm() {
    $("div#myModal .modal-body .row i:lt(1)").attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
    $("div#myModal .modal-body .row input:not([autocomplete])").val("").removeAttr("style");
    $("div#myModal .modal-body .row .col-sm-10 .wysiwyg-editor:not([autocomplete])").val("").removeAttr("style");
    //$("div#myModal .modal-body .row select:lt(1)").val("0").removeAttr("style");

    $("#but_saveadd").removeAttr("style");
    $("#but_savemodify").removeAttr("style").attr("style", "display:none");


}

//3.5  修改
//3.5.1 修改初始化
//$("#sample-table-2 i.icon-pencil").click(function () {
$("#sample-table-2 a.modify11").click(function () {
    //下拉框多了一个parent（）
        var idString = $(this).parent().parent().parent().parent().parent().children().eq(4).text();

    if (idString == "发送") {
        $.gritter.add({
            title: '消息提醒',
            text: '该通知已发送不能修改',
            class_name: 'gritter-info gritter-center'
        });
    }
    else {
        var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text()
        initForm();
        $.ajax({
            url: "LoadMessageModify",
            datatype: "json",
            type: "post",
            data: {
                id: id
            },
            success: function (data) {
                var json = eval(data);

                $("#idHidden").val(json.MSGID);
                $("#MSGID").val(json.MSGID);
                $("#MSGTitle").val(json.MSGTitle);
                //$("#editor1").val(json.MSGCountent);
                $("#editor1").html(json.MSGCountent);

                $("#Status").val(json.Status);

                //new PCAS("province", "city", "area", json.Province, json.City, json.County)


                $("#but_saveadd").attr("style", "display:none");
                $("#but_savemodify").removeAttr("style");
                $("#but_send").attr("style", "display:none");
                $("#but_modifysend").removeAttr("style");
                $("#myModal").modal();
            }

        });
    }
});


//3.6 发送
function send(id) {

    var idString = "";
    if (id == "1") {
        $("table tbody input[type='checkbox']:checked").parent().parent().next().each(function () {
            idString += $(this).text() + ","
        });

        idString = idString.substring(0, idString.length - 1);
    }
    else {
        //var idString = $("#sample-table-2 i.icon-share-alt").parent().parent().parent().parent().children().eq(1).text();
        idString = id;
    }
    if (idString.length == 0) {
        $.gritter.add({
            title: '消息提醒',
            text: '请选择要发送的项',
            class_name: 'gritter-info gritter-center'
        });
    }
    else {
        if (!confirm("确定发送")) { return; }
        $.ajax({
            url: "ToSend",
            dataType: "Json",
            data: { idString: idString },
            Type: "Post",
            success: function (data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    history.go(0);
                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        });
    }
}

//$("#sample-table-2 i.icon-share-alt").click(function () {
$("#sample-table-2 a.send11").click(function () {
    //下拉框多了一个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text()
    send(id);
});
$("#butsend").click(function sendCheck() {
    send(1);
}
);


//3.7   撤回
//$("#sample-table-2 i.icon-undo").click(function () {
$("#sample-table-2 a.back11").click(function () {
    //下拉框多了一个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text()
    backCheck(id);
});
$("#butgetback").click(function () {
    backCheck(1);
});

function backCheck(id) {
    var idString = "";
    if (id == "1") {
        $("table tbody input[type='checkbox']:checked").parent().parent().next().each(function () {
            idString += $(this).text() + ","
        });

        idString = idString.substring(0, idString.length - 1);
    }
    else {
        //var idString = $("#sample-table-2 i.icon-share-alt").parent().parent().parent().parent().children().eq(1).text();
        idString = id;
    }
    //var idString = $(this).parent().parent().parent().parent().children().eq(1).text();
    if (idString.length == 0) {
        $.gritter.add({
            title: '消息提醒',
            text: '请选择要撤回的项',
            class_name: 'gritter-info gritter-center'
        });
    }
    else {
        if (!confirm("确定撤回")) { return; }
        $.ajax({
            url: "ToGetBack",
            dataType: "Json",
            data: { idString: idString },
            Type: "Post",
            success: function (data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    history.go(0);
                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        });
    }
};

//3.0   功能
//3.1   删除
$("#butdel").click(function () {
    delCheck(1);
});
//$("#sample-table-2 i.icon-trash").click(function () {
$("#sample-table-2 a.delete11").click(function () {
    //下拉框多了一个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text();
    delCheck(id);
});
function delCheck(id) {
    var idString = "";
    if (id == 1) {
        $("table tbody input[type='checkbox']:checked").parent().parent().next().each(function () {
            idString += $(this).text() + ","
        });
        idString = idString.substring(0, idString.length - 1);
    }
    else {
        idString = id;
    }
    if (idString.length == 0) {
        $.gritter.add({
            title: '消息提醒',
            text: '请选择要删除的项',
            class_name: 'gritter-info gritter-center'
        });
    }
    else {
        if (!confirm("确定删除")) { return; }
        $.ajax({
            url: "ToDelete",
            dataType: "Json",
            data: { idString: idString },
            Type: "Post",
            success: function (data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    history.go(0);
                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        });
    }
}
